#!/bin/bash

# 测试新的推荐奖励机制
# 场景: A推荐了B，B充值并质押后产生收益，A应该获得B收益的10%

echo "================================"
echo "测试推荐奖励机制"
echo "================================"
echo ""

# 1. 创建管理员Token
echo "=== 1. 登录管理员账号 ==="
ADMIN_RESPONSE=$(curl -s -X POST http://localhost:3000/api/admin/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "username": "admin",
    "password": "admin123"
  }')

ADMIN_TOKEN=$(echo $ADMIN_RESPONSE | grep -o '"token":"[^"]*' | cut -d'"' -f4)
echo "管理员Token: $ADMIN_TOKEN"
echo ""

# 2. 注册用户A (推荐人)
echo "=== 2. 注册用户A (推荐人) ==="
USER_A_RESPONSE=$(curl -s -X POST http://localhost:3000/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "idNumber": "990101-1111111",
    "name": "用户A",
    "birthDateFirst6": "990101",
    "password": "password123",
    "withdrawalBank": "KB국민은행",
    "withdrawalAccount": "123-456-789",
    "accountHolder": "用户A",
    "mobileCarrier": "SKT",
    "mobileNumber": "010-1111-1111",
    "entryCode": "TEST001"
  }')

USER_A_ID=$(echo $USER_A_RESPONSE | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
USER_A_CODE=$(echo $USER_A_RESPONSE | grep -o '"referralCode":"[^"]*' | cut -d'"' -f4)
echo "用户A ID: $USER_A_ID"
echo "用户A推荐码: $USER_A_CODE"
echo ""

# 3. 注册用户B (被推荐人，使用A的推荐码)
echo "=== 3. 注册用户B (被推荐人) ==="
USER_B_RESPONSE=$(curl -s -X POST http://localhost:3000/api/auth/register \
  -H "Content-Type: application/json" \
  -d "{
    \"idNumber\": \"990202-2222222\",
    \"name\": \"用户B\",
    \"birthDateFirst6\": \"990202\",
    \"password\": \"password123\",
    \"withdrawalBank\": \"KB국민은행\",
    \"withdrawalAccount\": \"987-654-321\",
    \"accountHolder\": \"用户B\",
    \"mobileCarrier\": \"SKT\",
    \"mobileNumber\": \"010-2222-2222\",
    \"entryCode\": \"TEST001\",
    \"referralCode\": \"$USER_A_CODE\"
  }")

USER_B_ID=$(echo $USER_B_RESPONSE | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
USER_B_TOKEN=$(echo $USER_B_RESPONSE | grep -o '"token":"[^"]*' | cut -d'"' -f4)
echo "用户B ID: $USER_B_ID"
echo "用户B Token: $USER_B_TOKEN"
echo ""

# 4. 用户B申请充值1000 NEAR
echo "=== 4. 用户B申请充值 ==="
DEPOSIT_RESPONSE=$(curl -s -X POST http://localhost:3000/api/deposit/request \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $USER_B_TOKEN" \
  -d '{
    "amount": 100000000
  }')

DEPOSIT_ID=$(echo $DEPOSIT_RESPONSE | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
echo "充值申请ID: $DEPOSIT_ID"
echo ""

# 5. 管理员批准充值 (实际存入1000 NEAR)
echo "=== 5. 管理员批准充值 ==="
curl -s -X POST http://localhost:3000/api/admin/deposit/${DEPOSIT_ID}/process \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ADMIN_TOKEN" \
  -d '{
    "action": "confirm",
    "actualNearAmount": 1000,
    "note": "测试充值"
  }' | jq '.'
echo ""

# 6. 查看用户B的质押信息
echo "=== 6. 查看用户B的质押信息 ==="
echo "用户B质押本金应该是: 100 (注册赠送) + 1000 (充值) = 1100 NEAR"
curl -s http://localhost:3000/api/staking/info \
  -H "Authorization: Bearer $USER_B_TOKEN" | jq '{stakedAmount, totalRewardsNear, dailyRewardRate}'
echo ""

# 7. 手动触发收益计算 (计算当日收益)
echo "=== 7. 计算当日收益 ==="
echo "用户B的日收益 = 1100 NEAR * 0.2% = 2.2 NEAR"
curl -s -X POST http://localhost:3000/api/admin/test/calculate-daily-rewards \
  -H "Authorization: Bearer $ADMIN_TOKEN" | jq '.'
echo ""

# 8. 手动触发收益发放 (第1次发放)
echo "=== 8. 发放收益 (第1次) ==="
echo "用户B本次收益 = 2.2 / 12 ≈ 0.18 NEAR"
echo "用户A推荐奖励 = 0.18 * 10% = 0.018 NEAR"
curl -s -X POST http://localhost:3000/api/admin/test/distribute-rewards \
  -H "Authorization: Bearer $ADMIN_TOKEN" | jq '.'
echo ""

# 9. 查看用户A的推荐收益
echo "=== 9. 查看用户A的推荐收益 ==="
USER_A_LOGIN=$(curl -s -X POST http://localhost:3000/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "idNumber": "990101-1111111",
    "password": "password123"
  }')

USER_A_TOKEN=$(echo $USER_A_LOGIN | grep -o '"token":"[^"]*' | cut -d'"' -f4)

echo "用户A的质押信息 (lockedAmount应该有推荐奖励):"
curl -s http://localhost:3000/api/staking/info \
  -H "Authorization: Bearer $USER_A_TOKEN" | jq '{stakedAmount, lockedAmount, totalRewardsNear}'
echo ""

echo "用户A的推荐收益详情:"
curl -s http://localhost:3000/api/referral/earnings \
  -H "Authorization: Bearer $USER_A_TOKEN" | jq '.'
echo ""

# 10. 查看推荐奖励记录
echo "=== 10. 查看推荐奖励记录 ==="
curl -s http://localhost:3000/api/admin/referral/rewards \
  -H "Authorization: Bearer $ADMIN_TOKEN" | jq '.data.records[] | {userId, amountNear, source, createdAt}'
echo ""

echo "================================"
echo "测试完成!"
echo "================================"
echo ""
echo "验证要点:"
echo "1. ✅ 用户B充值后，A不应该获得充值推荐奖励 (已删除)"
echo "2. ✅ 用户B获得收益后，A应该获得B收益的10%"
echo "3. ✅ 推荐奖励存入A的lockedAmount"
echo "4. ✅ 每次收益发放时都会同时发放推荐奖励"
